package com.hermit.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hermit.system.dao.BbsAdministratorDao;
import com.hermit.system.dao.BbsMemberDao;
import com.hermit.system.entity.BbsAdministratorEntity;
import com.hermit.system.entity.BbsMemberEntity;
import com.hermit.system.service.LoginService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("loginService")
public class LoginServiceImpl extends ServiceImpl<BbsAdministratorDao, BbsAdministratorEntity> implements LoginService {
    private final Logger logger = LoggerFactory.getLogger(getClass());


    @Autowired
    private BbsAdministratorDao bbsAdministratorDao;
    @Autowired
    private BbsMemberDao bbsMemberDao;

    /**
     * 登录
     */
    public BbsAdministratorEntity login(Map<String, Object> params) {
        QueryWrapper<BbsAdministratorEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", params.get("username"));
        queryWrapper.eq("password", params.get("password"));
        queryWrapper.eq("delete_flag", 0);
        List<BbsAdministratorEntity> bbsAdministratorEntities = bbsAdministratorDao.query(queryWrapper);
        if (bbsAdministratorEntities == null || bbsAdministratorEntities.size() != 1) {
            // 管理员表没有查到信息,查询会员表
            QueryWrapper<BbsMemberEntity> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("username", params.get("username"));
            queryWrapper1.eq("password", params.get("password"));
            queryWrapper1.eq("delete_flag", 0);
            List<BbsMemberEntity> bbsMemberEntities = bbsMemberDao.query(queryWrapper1);
            if (bbsMemberEntities == null || bbsMemberEntities.size() != 1) {
                BbsAdministratorEntity bbsAdministratorEntity = new BbsAdministratorEntity();
                return bbsAdministratorEntity;
            } else {
                System.out.println("会员登录成功:" + bbsMemberEntities.get(0));
                BbsAdministratorEntity bbsAdministratorEntity = new BbsAdministratorEntity();
                bbsAdministratorEntity.setId(bbsMemberEntities.get(0).getId());
                bbsAdministratorEntity.setName(bbsMemberEntities.get(0).getName());
                bbsAdministratorEntity.setUsername(bbsMemberEntities.get(0).getUsername());
                bbsAdministratorEntity.setPassword(bbsMemberEntities.get(0).getPassword());
                bbsAdministratorEntity.setLoginType(1);
                return bbsAdministratorEntity;
            }
        } else {
            System.out.println("管理员登录成功:" + bbsAdministratorEntities.get(0));
            bbsAdministratorEntities.get(0).setLoginType(0);
            return bbsAdministratorEntities.get(0);
        }
    }

}
